import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, FormBuilder, Validators } from '@angular/forms';
import { LoginService } from '../service/login.service';
import { LoginForm } from 'src/app/common/type';
import { Utils } from 'src/app/common/utils';
import { DomSanitizer } from '@angular/platform-browser';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  public validateForm: FormGroup;

  public captcha:any;

  private uuid: string;

  constructor(private fb: FormBuilder,
    private domSanitizer: DomSanitizer,
    private loginService: LoginService) { }

  ngOnInit() {

    this.loginService.getCaptcha({ uuid: Utils.uuid() }).subscribe(res => {
      this.captcha = this.domSanitizer.bypassSecurityTrustScript(res.captcha);
      console.log(this.captcha);
    });
    this.validateForm = this.fb.group({
      username: [null, [Validators.required]],
      password: [null, [Validators.required]],
      remember: [true],
      captcha: [null, [Validators.required]]
    });
  }


  public submitForm() {
    const data: LoginForm = {
      username: this.validateForm.controls['username'].value,
      password: '',
      uuid: '',
      captcha: ''
    };
    console.log(data);
    this.loginService.login(data);
  }

}
